﻿#include "GUIGeometryBoolDialog.h"
#include "ui_GUIGeometryBoolDialog.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperLine.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelPoint.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSurface.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "FITK_Component/FITKGeoCompOCC/FITKOCCOperBool.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperCompound.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoEnum.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoMultiDelete.h"

namespace GUI {

    GUIGeometryBoolDialog::GUIGeometryBoolDialog(Core::FITKActionOperator* oper, Interface::FITKAbsGeoOperBool::GeoBoolOperType type, QWidget* parent)
        : GUIGeometryBaseDialog(nullptr, oper, parent)
    {
        _ui = new Ui::GUIGeometryBoolDialog();
        _ui->setupUi(this);
        this->createGeometryObj(Interface::FITKGeoEnum::FITKGeometryComType::FGTBool);

        switch (type)
        {
        case Interface::FITKAbsGeoOperBool::GBTAdd:
            setWindowTitle("Unite");
            break;
        case Interface::FITKAbsGeoOperBool::GBTCommon:
            setWindowTitle("Intersect");
            break;
        case Interface::FITKAbsGeoOperBool::GBTCut:
            setWindowTitle("Subtract");
            break;
        default:
            break;
        }

        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        this->init();
    }

    GUIGeometryBoolDialog::~GUIGeometryBoolDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUIGeometryBoolDialog::init()
    {
        
        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();
        if (!geometryData) return;
        // 获取默认名称
        if (this->windowTitle()== "Unite")
        {
            QString name = geometryData->checkName(tr("Unite-1"));
            _ui->lineEdit_Name->setText(name);
        }
        else if (this->windowTitle() == "Intersect")
        {
            QString name = geometryData->checkName(tr("Intersect-1"));
            _ui->lineEdit_Name->setText(name);
        }
        else if (this->windowTitle() == "Subtract")
        {
            QString name = geometryData->checkName(tr("Subtract-1"));
            _ui->lineEdit_Name->setText(name);
        }
    }

    QString GUIGeometryBoolDialog::getNameText()
    {
        return _ui->lineEdit_Name->text();
    }

    void GUIGeometryBoolDialog::pickFinishedOper()
    {
        //获取拾取数据
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(pickFinishedOper()));
        // 处理拾取数据
        QList<GraphData::PickedData*> pickList = provider->getPickedList();
        if (pickList.size() == 1) {
            GraphData::PickedData* pickData = pickList.at(0);
            if (pickData == nullptr) return;
            // 获取虚拓扑形状
            QList<int> virtualShapesID = pickData->getPickedIds();
            QHash<int, int> pickOther = pickData->getPickedOtherIDs();
            if (virtualShapesID.size() == 0 || !pickOther.contains(virtualShapesID.at(0))) return;
            if (_flag == 1)
            {
                _virtualTarget.VirtualTopoId = virtualShapesID.at(0);
                _virtualTarget.VirtualTopoIndex = pickOther.value(virtualShapesID.at(0));
                _virtualTarget.CmdId = pickData->getPickedDataObjId();
                _virtualTarget.Type = Interface::FITKGeoEnum::VTopoShapeType::VSSolid;
            }
            else if (_flag == 2)
            {
                _virtualTool.VirtualTopoId = virtualShapesID.at(0);
                _virtualTool.VirtualTopoIndex = pickOther.value(virtualShapesID.at(0));
                _virtualTool.CmdId = pickData->getPickedDataObjId();
                _virtualTool.Type = Interface::FITKGeoEnum::VTopoShapeType::VSSolid;
            }
        }
        _ui->label_Curve1->setText(_virtualTarget.isNull() ? tr("Picked (0)") : tr("Picked (1)"));
        _ui->label_Curve2->setText(_virtualTool.isNull() ? tr("Picked (0)") : tr("Picked (1)"));
    }

    void GUIGeometryBoolDialog::getDataFormWidget()
    {        
        OCC::FITKOCCOperBool *boolOper= dynamic_cast<OCC::FITKOCCOperBool *>(_obj);
        
        boolOper->setTarget(_virtualTarget);
        boolOper->setTool(_virtualTool);
        // 设置布尔运算类型
        if (this->windowTitle() == "Unite")
            boolOper->setBoolOperType(Interface::FITKAbsGeoOperBool::GBTAdd);
        else if (this->windowTitle() == "Intersect")
            boolOper->setBoolOperType(Interface::FITKAbsGeoOperBool::GBTCommon);
        else if (this->windowTitle() == "Subtract")
            boolOper->setBoolOperType(Interface::FITKAbsGeoOperBool::GBTCut);

        boolOper->clearReference();
        boolOper->addReferenceCmdID(_virtualTarget.CmdId);
        if(_virtualTarget.CmdId != _virtualTool.CmdId)
            boolOper->addReferenceCmdID(_virtualTool.CmdId);
    }

    void GUIGeometryBoolDialog::on_pushButton_Pick_clicked()
    {
        //拾取对象 几何体
        //拾取方式 单独数据拾取方式。（点选+追加拾取）    
        _flag = 1;
        enablePickSig(GUI::GUIPickInfo::POBJGeometrySolid, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryBoolDialog::on_pushButton_Pick_2_clicked()
    {
        //拾取对象 几何体
        //拾取方式 单独数据拾取方式。（点选+追加拾取）    
        _flag = 2;
        enablePickSig(GUI::GUIPickInfo::POBJGeometrySolid, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryBoolDialog::on_pushButton_OK_clicked()
    {        
        if (_virtualTarget.isNull() == true || _virtualTool.isNull() == true) return;
        if (_isCreate)
            if (!this->createObjData()) return;
        else
            if (!this->editObjData()) return;

        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    void GUIGeometryBoolDialog::on_pushButton_Cancel_clicked()
    {
        this->close();
    }
}